<?php

class Schedule extends MY_Controller {

		function __construct()
		{
			parent::__construct();
			
		    $this->page=$this->module_codes['Schedule'];
			$this->load->model('schedule_model' , 'SM');
			$this->data['user'] = $this->get_user(true);
			$this->data['patient'] = $this->get_patient(true);
			
			$this->prefs['template'] = '

						   {table_open}<table border="0" cellpadding="0" cellspacing="0" class="calendar">{/table_open}
						   {heading_row_start}<tr>{/heading_row_start}
							   {heading_previous_cell}<th><a href="{previous_url}">&lt;&lt;</a></th>{/heading_previous_cell}
							   {heading_title_cell}<th colspan="{colspan}"><div class="ui-datepicker-header ui-widget-header ui-helper-clearfix ui-corner-all">{heading}</div></th>{/heading_title_cell}
							   {heading_next_cell}<th><a href="{next_url}">&gt;&gt;</a></th>{/heading_next_cell}
						   {heading_row_end}</tr>{/heading_row_end}
						   {week_row_start}<tr>{/week_row_start}
							   {week_day_cell}<td>{week_day}</td>{/week_day_cell}
						   {week_row_end}</tr>{/week_row_end}
						   {cal_row_start}<tr>{/cal_row_start}
							   {cal_cell_start}<td>{/cal_cell_start}
							   {cal_cell_content}<span class="ui-state-default ui-state-highlight" ><a title="{content}" href="{content}" onclick="open_the_day(this.title); return false;">{day}</a></span>{/cal_cell_content}
							   
							   {cal_cell_no_content}<span class="ui-state-default" >{day}</span>{/cal_cell_no_content}
							   {cal_cell_no_content_today}<span class="ui-state-default ui-state-hover" >{day}</span>{/cal_cell_no_content_today}
							   {cal_cell_blank}&nbsp;{/cal_cell_blank}
							   {cal_cell_end}</td>{/cal_cell_end}
						   {cal_row_end}</tr>{/cal_row_end}
						   {table_close}</table>{/table_close}
						';
		}
		
		function get_checkup($as_choices = false)
		{
		//get all types
		
            $this->check_page_permission('access');
		$this->load->model('list_price_model' , 'LPM');
			$result = $this->LPM->get_list_price();
			if($as_choices){
				$arr = array();
				foreach($result['info'] as $row){
					$arr[$row['id'] ] = $row['test_name'];
				}
				//print_r($arr);
				return $arr;
			}
			//return $result['info'];
		}
		
		function get_patient($as_choices = false)
		{
		//get all types
		
            $this->check_page_permission('access');
		$this->load->model('patient_model' , 'PM');
		
			$result = $this->PM->get_patient_data();
			if($as_choices){
				$arr = array();
				foreach($result['info'] as $row){
					$arr[$row['id'] ] = $row['first_name'].' '.$row['last_name'];
				}
				//print_r($arr);
				return $arr;
			}
			//return $result['info'];
		}
		
		function get_user($as_choices = false)
		{
			
            $this->check_page_permission('access');
		//get all types
		
		$this->load->model('user_model' , 'USER');
			$result = $this->USER->get_user_data();
			if($as_choices){
				$arr = array();
				foreach($result['info'] as $row){
					$arr[$row['id'] ] = $row['first_name'].' '.$row['last_name'];
				}
				//print_r($arr);
				return $arr;
			}
			//return $result['info'];
		}
		
		function index()
		{
			
			
            $this->check_page_permission('access');
			$this->load->library('calendar', $this->prefs);
			$result = $this->SM->get_appointment();
			$this_month = array();
			$next_1month = array();
			$next_2month = array();
			//if $result['count'] = 0 then there is no data
			//else generate array for all patient(s)
			if($result['count'] == 0){
				$data['count'] = 0;
			}
			else{
				$data['count'] = $result['count'];
				$data['result'] = $result['info'];
				
				foreach($result['info'] as $val=>$val2){
					if(date("m")==$val2['month'])
						$this_month[$val2['day']] = $val2['day'].'/'.$val2['month'].'/'.$val2['year'];
				}
				
				foreach($result['info'] as $val=>$val2){
					if(date("m")+1==$val2['month'])
						$next_1month[$val2['day']] = $val2['day'].'/'.$val2['month'].'/'.$val2['year'];
				}
				
				foreach($result['info'] as $val=>$val2){
					if(date("m")+2==$val2['month'])
						$next_2month[$val2['day']] = $val2['day'].'/'.$val2['month'].'/'.$val2['year'];
				}

			
			}
			$data['cal0'] = $this->calendar->generate(date("y"), date("m"), $this_month);
			$data['cal1'] = $this->calendar->generate(date("y"), date("m")+1, $next_1month);
			$data['cal2'] = $this->calendar->generate(date("y"), date("m")+2, $next_2month);
			//print_r($this->data);
			$this->load->view('head');
			$this->load->view('schedule/main', array_merge($this->data, $data));
			$this->load->view('footer');
		}

		function new_apointment($id=NULL)
		{
			
            $this->check_page_permission('add');
			$apointment_info	 	= $id!==NULL ? $this->SM->get_appointment($id) : array();
			$apointment_info['date']=!empty($apointment_info)?$apointment_info['day'].'/'.$apointment_info['month'].'/'.$apointment_info['year'].' '.' '.$apointment_info['time']:'';
			
			if($id!==NULL){
			$this->load->model('patient_model' , 'PM');
			$patient_info= $this->PM->get_patient_data($apointment_info['patient_id']);
			
			#print_r($patient_info);
			$apointment_info['patient_name']=$patient_info['first_name'].' '.$patient_info['last_name'];
			}
			$submit_data 			= $this->input->post('apointment_form');	
			$submit_data 			= !is_array($submit_data) ? array() : $submit_data;
			//print_r($apointment_info);
			$this->load->view('head');
			$this->load->view('schedule/appointment_form' , array_merge($this->data, array_merge($apointment_info,$submit_data)));
			$this->load->view('footer');	
		}
		
		function new_apointment2($id=NULL)
		{

			$data = $this->input->post('apointment_form');
			
			//collect the data
			//get the timestamp date
			//then change it to desire format
			$time = strtotime($data['date']);
			$db_time = date('h:i A',$time);
			
				
				$apointment['id'] 			= $data['id'];
				$apointment['date'] 			= $data['date'];

				list($apointment['month'], $apointment['day'], $apointment['year']) = explode("/", date('m/d/Y',$time));
				$apointment['time'] 		= $db_time;
				$apointment['patient_id'] 	= $data['patient_id'];
				$apointment['user_id'] 		= $data['user_id'];
				$apointment['added_by'] 	= $this->session->userdata('username');
				$apointment['note'] 		= $data['note'];
				$apointment['timestamp'] 		= strtotime($db_time);
				
			
			
			$this->form_validation->set_error_delimiters('<div class="error">', '</div>');
			
			if($id==NULL)$id =  $data['id'];
			if ($this->form_validation->run('apointment') == FALSE)
			{			
				return $this->new_apointment();
			}
			//Edit:
			if(isset($id) && $id!=NULL){
				
            $this->check_page_permission('edit');
					if($this->SM->update_apointment_by_id($apointment, $id)>0 )
					redirect('schedule/' , 'refresh');
					else{
                           echo "<script>alert('".lang('appoitment_exist')."');history.go(-1)</script>";					} 
				}
			else{
				

					
            $this->check_page_permission('add');
					//print_r($apointment);
					//die;
					
					if($this->SM->insert_new_apointment($apointment)>0)
						redirect('schedule/' , 'refresh');
					else {
                        echo "<script>alert('".lang('appoitment_exist')."');history.go(-1)</script>";
						
					}
				}
			
			
			
		}
		
		function show_day_details($day=NULL, $month=NULL, $year=NULL,$from_calender=0)
		{
                    //$this->data['patient']
                    
            $this->check_page_permission('access');
			$date['day'] 	=  $day;
			$date['month'] 	=  $month;
			$date['year'] 	=  $year;
			$result = $this->SM->get_appointment(NULL, $date);
		//	$this->SM->show_the_query();
			//echo "the count is:". count($result)."<br>";
			#echo "<pre>";
			//print_r($result);
			$details='';
			if($result['count'] == 0 && !$from_calender)
				$details.= lang('general_no_data_found');
			else{
				if(!$from_calender) $details.= $result['count'] .' '. lang('general_record_found').'<br><br>';
				$details.= '<script type="text/javascript">
						$(function() {
							var icons = {
								header: "ui-icon-circle-arrow-e",
								headerSelected: "ui-icon-circle-arrow-s"
							};
							$("#accordion").accordion({
								icons: icons
							});
							$("#toggle").button().toggle(function() {
								$("#accordion").accordion("option", "icons", false);
							}, function() {
								$("#accordion").accordion("option", "icons", icons);
							});
						});
						</script>
					';
				$details.= '<!-- Start --><div class=""><div id="accordion">';
				$counter = 0;
				
				while($counter<count($result)-1)
				{
				
						$details.= '<h3><a href="#">'.$result[$counter]['time'].' - '.$result[$counter]['day'].'/'. $result[$counter]['month'].'/'. $result[$counter]['year'].'</a></h3>';
						$details.= '<div>
                                                   <b> '.lang('menuitem_patient').' :</b> '.$this->data['patient'][$result[$counter]['patient_id']].'
                                                        <br><b>
                                                        '.lang('general_soctor').' :</b> '.$this->data['user'][$result[$counter]['user_id']].'
                                                           <br>'. anchor("patient/patient_history/".$result[$counter]['patient_id'] , lang('form_label_history')) .' - '. anchor("patient/patient_details/".$result[$counter]['patient_id'] , lang('form_label_details')).' - '. anchor("schedule/new_apointment/".$result[$counter]['id'] , lang('menuitem_postpone'))  ;
						#echo "</br>".$result[$counter]['day'];
						#echo "</br>".date('j') ;
						#echo "</br>".$result[$counter]['month'];
						#echo "</br>".date('n'); 
						#echo "</br>".$result[$counter]['year'];
						#echo "</br>".date('Y');
						
						if($result[$counter]['day']==date('j') && $result[$counter]['month']==date('n') && $result[$counter]['year']==date('Y') )
						$details.='<br><br>'. anchor("preview/new_preview/".$result[$counter]['patient_id'], lang('menuitem_preview')) ;
                                                       $details.=' </div>';
                                                //print_r();
						$counter++;
				}
				$details.= '</div></div><!-- End -->';
			
			
			
			}
			
			 echo $details;
			
			/*
			echo "<pre>";
			print_r($result);
			echo "</pre>";

                        */
		}
	

	}
/* End of file schedule.php */
/* Location: ./system/application/controllers/schedule.php */
